Code Example (Week 5)
module Week4 where
data Tree a
= Leaf
| Node a (Tree a) (Tree a)
deriving (Show)
instance Functor Tree where
fmap f Leaf = Leaf
fmap f (Node x t1 t2)
= Node (f x) (fmap f t1) (fmap f t2)
fakeMap :: (a ->b) -> [a] -> [b]
fakeMap f [] = undefined
fakeMap f (x : xs) = undefined
fappM :: Maybe (a -> b) -> Maybe a -> Maybe b
fappM Nothing _ = Nothing
fappM _ Nothing = Nothing
fappM (Just f) (Just x) = Just ( f x)
fappL :: [a -> b] -> [a] -> [b]
fappL [] xs = []
fappL _ [] = []
fappL (f : fs) xs
= map f xs ++ fappL fs xs
addMaybe1
:: Num a => Maybe a -> Maybe a -> Maybe a
addMaybe1 (Just x) (Just y) = Just (x+y)
addM1 mx my =
mx >>= \x ->
my >>= \y ->
return (x + y)
addM2 mx my = do
{ x <- mx
; y <- my
; return (x + y)
}